// Store/index.js
import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    products: [
      { id: 1, name: '华为手表', price: 5, description: '时尚佩戴', imageUrl: 'path/to/image' },
      { id: 2, name: '手表', price: 60, description: '时尚佩戴', imageUrl: 'path/to/image' },
      { id: 3, name: '华为', price: 100, description: '时尚佩戴', imageUrl: 'path/to/image' },
      { id: 4, name: 'ROG幻16', price: 10, description: '游戏娱乐', imageUrl: 'path/to/image' }
    ],
    orders: [],
    cartItems: [],
    wishlistItems: [],
    isLoggedIn: false,
    currentUser: null
  },
  mutations: {
    SET_USER_LOGGED_IN(state, payload) {
      state.isLoggedIn = true;
      state.currentUser = payload.currentUser;
      // 从本地存储加载用户特定的购物车、收藏夹和订单数据
      state.cartItems = JSON.parse(localStorage.getItem(`${payload.currentUser}_cartItems`)) || [];
      state.wishlistItems = JSON.parse(localStorage.getItem(`${payload.currentUser}_wishlistItems`)) || [];
      state.orders = JSON.parse(localStorage.getItem(`${payload.currentUser}_orders`)) || [];
    },
    CLEAR_USER_DATA(state) {
      state.isLoggedIn = false;
      state.currentUser = null;
      state.cartItems = [];
      state.wishlistItems = [];
      state.orders = [];
      // 清除本地存储中的数据
      localStorage.removeItem(`${state.currentUser}_cartItems`);
      localStorage.removeItem(`${state.currentUser}_wishlistItems`);
      localStorage.removeItem(`${state.currentUser}_orders`);
    },
    ADD_TO_CART(state, product) {
      const existingProduct = state.cartItems.find(item => item.id === product.id);
      if (existingProduct) {
        existingProduct.quantity += 1;
      } else {
        state.cartItems.push({ ...product, quantity: 1 });
      }
      // 更新本地存储
      localStorage.setItem(`${state.currentUser}_cartItems`, JSON.stringify(state.cartItems));
    },
    REMOVE_FROM_CART(state, product) {
      const index = state.cartItems.findIndex(item => item.id === product.id);
      if (index !== -1) {
        state.cartItems.splice(index, 1);
      }
      // 更新本地存储
      localStorage.setItem(`${state.currentUser}_cartItems`, JSON.stringify(state.cartItems));
    },
    ADD_TO_WISHLIST(state, product) {
      const existingProduct = state.wishlistItems.find(item => item.id === product.id);
      if (!existingProduct) {
        state.wishlistItems.push({ ...product, quantity: 1 });
      }
      // 更新本地存储
      localStorage.setItem(`${state.currentUser}_wishlistItems`, JSON.stringify(state.wishlistItems));
    },
    REMOVE_FROM_WISHLIST(state, productId) {
      const index = state.wishlistItems.findIndex(item => item.id === productId);
      if (index !== -1) {
        state.wishlistItems.splice(index, 1);
      }
      // 更新本地存储
      localStorage.setItem(`${state.currentUser}_wishlistItems`, JSON.stringify(state.wishlistItems));
    },
    ADD_ORDER(state, order) {
      state.orders.push(order);
      localStorage.setItem(`${state.currentUser}_orders`, JSON.stringify(state.orders));
    }
  },
  actions: {
    login({ commit }, username) {
      commit('SET_USER_LOGGED_IN', { currentUser: username });
    },
    logout({ commit }) {
      commit('CLEAR_USER_DATA');
    },
    addToCart({ commit }, product) {
      commit('ADD_TO_CART', product);
    },
    removeFromCart({ commit }, product) {
      commit('REMOVE_FROM_CART', product);
    },
    addToWishlist({ commit }, product) {
      commit('ADD_TO_WISHLIST', product);
    },
    removeFromWishlist({ commit }, productId) {
      commit('REMOVE_FROM_WISHLIST', productId);
    },
    addOrder({ commit }, order) {
      commit('ADD_ORDER', order);
    }
  }
});